const authApi = require('../../../api/auth')
const app = getApp()

Page({
  data: {
    username: '',
    phone: '',
    email: '',
    password: '',
    confirmPassword: '',
    showPassword: false,
    showConfirmPassword: false,
    isAgreed: false,
    isFormValid: false,
    validationMessage: '',
    validationType: '' // error, success, warning
  },

  onLoad: function (options) {
    // 检查是否已经登录
    if (app.globalData.isLogin) {
      wx.switchTab({
        url: '/pages/index/index'
      })
    }
  },

  // 用户名输入事件
  onUsernameInput(e) {
    this.setData({
      username: e.detail.value
    })
    this.validateForm()
  },

  // 手机号输入事件
  onPhoneInput(e) {
    this.setData({
      phone: e.detail.value
    })
    this.validateForm()
  },

  // 邮箱输入事件
  onEmailInput(e) {
    this.setData({
      email: e.detail.value
    })
    this.validateForm()
  },

  // 密码输入事件
  onPasswordInput(e) {
    this.setData({
      password: e.detail.value
    })
    this.validateForm()
  },

  // 确认密码输入事件
  onConfirmPasswordInput(e) {
    this.setData({
      confirmPassword: e.detail.value
    })
    this.validateForm()
  },

  // 切换密码显示状态
  togglePassword() {
    this.setData({
      showPassword: !this.data.showPassword
    })
  },

  // 切换确认密码显示状态
  toggleConfirmPassword() {
    this.setData({
      showConfirmPassword: !this.data.showConfirmPassword
    })
  },

  // 用户协议选择事件
  onAgreementChange(e) {
    const isAgreed = e.detail.value.includes('agree')
    this.setData({
      isAgreed: isAgreed
    })
    this.validateForm()
  },

  // 表单验证
  validateForm() {
    const { username, phone, email, password, confirmPassword, isAgreed } = this.data

    // 重置验证信息
    let validationMessage = ''
    let validationType = ''
    let isFormValid = false

    // 检查必填字段
    if (!username.trim()) {
      validationMessage = '请输入用户名'
      validationType = 'error'
    } else if (username.trim().length < 3) {
      validationMessage = '用户名至少需要3个字符'
      validationType = 'error'
    } else if (!phone.trim()) {
      validationMessage = '请输入手机号'
      validationType = 'error'
    } else if (!this.isValidPhone(phone)) {
      validationMessage = '请输入正确的手机号格式'
      validationType = 'error'
    } else if (!email.trim()) {
      validationMessage = '请输入邮箱地址'
      validationType = 'error'
    } else if (!this.isValidEmail(email)) {
      validationMessage = '请输入正确的邮箱格式'
      validationType = 'error'
    } else if (!password.trim()) {
      validationMessage = '请设置密码'
      validationType = 'error'
    } else if (password.length < 6) {
      validationMessage = '密码至少需要6位'
      validationType = 'error'
    } else if (password.length > 20) {
      validationMessage = '密码不能超过20位'
      validationType = 'error'
    } else if (!confirmPassword.trim()) {
      validationMessage = '请确认密码'
      validationType = 'error'
    } else if (password !== confirmPassword) {
      validationMessage = '两次输入的密码不一致'
      validationType = 'error'
    } else if (!isAgreed) {
      validationMessage = '请阅读并同意用户协议'
      validationType = 'error'
    } else {
      validationMessage = '信息填写正确'
      validationType = 'success'
      isFormValid = true
    }

    this.setData({
      validationMessage,
      validationType,
      isFormValid
    })
  },

  // 验证手机号格式
  isValidPhone(phone) {
    const phoneReg = /^1[3-9]\d{9}$/
    return phoneReg.test(phone)
  },

  // 验证邮箱格式
  isValidEmail(email) {
    const emailReg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
    return emailReg.test(email)
  },

  // 处理注册
  async handleRegister() {
    if (!this.data.isFormValid) {
      return
    }

    const { username, phone, email, password } = this.data

    try {
      await authApi.register({
        username: username.trim(),
        phone: phone.trim(),
        email: email.trim(),
        password: password.trim()
      })

      wx.showToast({
        title: '注册成功',
        icon: 'success'
      })

      // 延迟跳转到登录页面
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/auth/login/login'
        })
      }, 1500)

    } catch (error) {
      console.error('注册失败:', error)
      wx.showToast({
        title: error.message || '注册失败',
        icon: 'none'
      })
    }
  },

  // 跳转到登录页面
  goToLogin() {
    wx.redirectTo({
      url: '/pages/auth/login/login'
    })
  },

  // 显示用户协议
  showUserAgreement() {
    wx.showModal({
      title: '用户协议',
      content: '这里是用户协议的内容...\n\n1. 用户需遵守相关法律法规\n2. 保护个人账号安全\n3. 不得发布违法违规内容\n4. 尊重他人权益\n\n详细内容请访问我们的官网查看。',
      showCancel: false,
      confirmText: '我知道了'
    })
  },

  // 显示隐私政策
  showPrivacyPolicy() {
    wx.showModal({
      title: '隐私政策',
      content: '这里是隐私政策的内容...\n\n1. 我们重视您的隐私保护\n2. 收集必要的个人信息\n3. 不会泄露您的个人信息\n4. 严格遵守相关法律法规\n\n详细内容请访问我们的官网查看。',
      showCancel: false,
      confirmText: '我知道了'
    })
  }
}) 